<?php
/**
 * 易居CMS
 * ============================================================================
 * 版权所有 2018-2028 海南易而优科技有限公司，并保留所有权利。
 * 网站地址: http://www.ejucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-1-7
 */

namespace app\admin\controller;

use think\Page;
use think\Db;

class Xinfang extends Base
{
    // 模型标识
    public $nid = 'xinfang';
    // 模型ID
    public $channeltype = 9;
    // 模型附加表
    public $table = 'Xinfang';
    // 模型信息
    public $channeltype_info;

    /*
     * 初始化操作
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->archives_db = Db::name('archives');
        $channeltype_list = config('global.channeltype_list');
        $this->channeltype = $channeltype_list[$this->nid];
        $this->assign('nid', $this->nid);
        $this->assign('channeltype', $this->channeltype);
        $this->channeltype_info = Db::name('channeltype')->field('id,ntitle')->find($this->channeltype);
        $this->assign('channeltype_info', $this->channeltype_info);
    }

    /**
     * 内容列表动态获取数据（包括html和数据）
     */
    public function getAjaxHtml(){
        $assign_data = array();
        $condition = array();
        // 获取到所有GET参数
        $param = input('param.');
        $typeid = input('typeid/d', 0);
        $begin = strtotime(input('add_time_begin'));
        $end = strtotime(input('add_time_end'));

        $admin_info = session('admin_info');
        $auth_role_info = [];
        if (0 < intval($admin_info['role_id'])) {
            $auth_role_info = $admin_info['auth_role_info'];
            if(! empty($auth_role_info)){
                if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
                    $condition['a.admin_id'] = $admin_info['admin_id'];
                }
                if(!empty($auth_role_info['permission']['arctype'])){
                    $condition['a.typeid'] = array('IN', $auth_role_info['permission']['arctype']);
                }
            }
        }
        // 应用搜索条件
        foreach (['keywords','typeid','flag'] as $key) {
            if (isset($param[$key]) && $param[$key] !== '') {
                if ($key == 'keywords') {
                    $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
                } else if ($key == 'typeid') {
                    $typeid = $param[$key];
                    $hasRow = model('Arctype')->getHasChildren($typeid);
                    $typeids = get_arr_column($hasRow, 'id');
                    if(!empty($auth_role_info['permission']['arctype']) && !empty($typeids)){
                        $typeids = array_intersect($typeids, $auth_role_info['permission']['arctype']);
                    }
                    $condition['a.typeid'] = array('IN', $typeids);
                } else if ($key == 'flag') {
                    $condition['a.'.$param[$key]] = array('eq', 1);
                } else {
                    $condition['a.'.$key] = array('eq', $param[$key]);
                }
            }
        }
        // 时间检索
        if ($begin > 0 && $end > 0) {
            $condition['a.add_time'] = array('between',"$begin,$end");
        } else if ($begin > 0) {
            $condition['a.add_time'] = array('egt', $begin);
        } else if ($end > 0) {
            $condition['a.add_time'] = array('elt', $end);
        }
        // 模型ID
        $condition['a.channel'] = array('eq', $this->channeltype);
        // 回收站
        $condition['a.is_del'] = array('eq', 0);
        /**
         * 数据查询，搜索出主键ID的值
         */
        $count = DB::name('archives')->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数
        $Page = new Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数
        $list = DB::name('archives')
            ->field("a.aid")
            ->alias('a')
            ->where($condition)
            ->order('a.aid desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->getAllWithIndex('aid');

        /**
         * 完善数据集信息
         * 在数据量大的情况下，经过优化的搜索逻辑，先搜索出主键ID，再通过ID将其他信息补充完整；
         */
        if ($list) {
            $aids = array_keys($list);
            $fields = "d.*,c.*,b.*, a.*, a.aid as aid";
            $row =  DB::name('archives')
                ->field($fields)
                ->alias('a')
                ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
                ->join("xinfang_content c","a.aid = c.aid","LEFT")
                ->join("xinfang_system d","a.aid = d.aid","LEFT")
                ->where('a.aid', 'in', $aids)
                ->getAllWithIndex('aid');
            foreach ($list as $key => $val) {
                $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
                $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
                $list[$key] = $row[$val['aid']];
            }
        }
        $show = $Page->show(); // 分页显示输出
        $assign_data['page'] = $show; // 赋值分页输出
        $assign_data['list'] = $list; // 赋值数据集
        $assign_data['pager'] = $Page; // 赋值分页对象
        // 栏目ID
        $assign_data['typeid'] = $typeid; // 栏目ID

        $this->assign($assign_data);

        return $this->fetch('ajax_index');
    }

    /**
     * ajax获取列表数据
     */
    public function ajax_get_list(){
        $condition['a.channel'] = array('eq', $this->channeltype);
        $condition['a.is_del'] = array('eq', 0);
        $admin_info = session('admin_info');
        if (0 < intval($admin_info['role_id'])) {
            $auth_role_info = $admin_info['auth_role_info'];
            if(! empty($auth_role_info)){
                if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
                    $condition['a.admin_id'] = $admin_info['admin_id'];
                }
                if(!empty($auth_role_info['permission']['arctype'])){
                    $condition['a.typeid'] = array('IN', $auth_role_info['permission']['arctype']);
                }
            }
        }
        $count = DB::name('archives')->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数
        $Page = new Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数  config('paginate.list_rows')
        $list = DB::name('archives')
            ->field("a.aid")
            ->alias('a')
            ->where($condition)
            ->order('a.add_time desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->getAllWithIndex('aid');
        if ($list) {
            $aids = array_keys($list);
            $fields = "b.*, a.*, a.aid as aid";
            $row = DB::name('archives')
                ->field($fields)
                ->alias('a')
                ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
                ->where('a.aid', 'in', $aids)
                ->order('a.add_time desc')
                ->getAllWithIndex('aid');
            foreach ($list as $key => $val) {
                $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
                $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
                $openurl = url('Archives/edit',[/*'typeid'=>$row[$val['aid']]['typeid'],*/ 'id'=>$val['aid']]);//urlencode(url('Archives/edit',['typeid'=>$val['typeid'],'id'=>$val['aid']]));
                $row[$val['aid']]['lay_href'] = url('Xinfang/index',[/*'typeid'=>$row[$val['aid']]['typeid'],*/ 'msg'=>'编辑','openurl'=>$openurl]);


                $list[$key] = $row[$val['aid']];
            }
        }
        $list = array_merge($list);
        return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$list];
    }

    /**
     * 列表
     */
    public function index()
    {
        $assign_data = array();
        $condition = array();
        // 获取到所有GET参数
        $param = input('param.');
        $flag = input('flag/s');
        $typeid = input('typeid/d', 0);
        /*权限控制 by 小虎哥*/
        $admin_info = session('admin_info');
        $auth_role_info = [];
        if (0 < intval($admin_info['role_id'])) {
            $auth_role_info = $admin_info['auth_role_info'];
            if(! empty($auth_role_info)){
                if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
                    $condition['a.admin_id'] = $admin_info['admin_id'];
                }
                if(!empty($auth_role_info['permission']['arctype'])){
                    $condition['a.typeid'] = array('IN', $auth_role_info['permission']['arctype']);
                }
            }
        }
        /*--end*/
        // 应用搜索条件
        foreach (['keywords','typeid','flag'] as $key) {
            if (isset($param[$key]) && $param[$key] !== '') {
                if ($key == 'keywords') {
                    $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
                } else if ($key == 'typeid') {
                    $typeid = $param[$key];
                    $hasRow = model('Arctype')->getHasChildren($typeid);
                    $typeids = get_arr_column($hasRow, 'id');
                    if(!empty($auth_role_info['permission']['arctype']) && !empty($typeids)){
                        $typeids = array_intersect($typeids, $auth_role_info['permission']['arctype']);
                    }
                    $condition['a.typeid'] = array('IN', $typeids);
                } else if ($key == 'flag') {
                    $condition['a.'.$param[$key]] = array('eq', 1);
                } else {
                    $condition['a.'.$key] = array('eq', $param[$key]);
                }
            }
        }

        $begin = strtotime(input('add_time_begin'));
        $end = strtotime(input('add_time_end'));
        // 时间检索
        if ($begin > 0 && $end > 0) {
            $condition['a.add_time'] = array('between',"$begin,$end");
        } else if ($begin > 0) {
            $condition['a.add_time'] = array('egt', $begin);
        } else if ($end > 0) {
            $condition['a.add_time'] = array('elt', $end);
        }
        //省份搜索
        $province_id = input('province_id/d',0);
        $city_id = input('city_id/d',0);
        //城市搜索
        if (!empty($city_id)){
            $condition['city_id'] = $city_id;
            if (empty($province_id)){
                $province_id = model('region')->where("id=".$city_id)->getField('parent_id');
            }
        }else if (!empty($province_id)){
            $condition['province_id'] = $province_id;
        }
        $assign_data['province_id'] = $province_id;
        $assign_data['city_id'] = $city_id;

        //状态搜索
        $sale_status = input('sale_status',"");
        if (!empty($sale_status)){
            $condition['c.sale_status'] = $sale_status;
        }

        // 模型ID
        $condition['a.channel'] = array('eq', $this->channeltype);

        // 回收站
        $condition['a.is_del'] = array('eq', 0);

        /**
         * 数据查询，搜索出主键ID的值
         */
        $count = $this->archives_db->alias('a')
            ->join("xinfang_system c","a.aid=c.aid","LEFT")
            ->where($condition)->count('a.aid');// 查询满足要求的总记录数
        unset($param['openurl']);
        $Page = new Page($count,config('paginate.list_rows'),$param);// 实例化分页类 传入总记录数和每页显示的记录数  config('paginate.list_rows')
        $list = $this->archives_db
            ->join("xinfang_system c","a.aid=c.aid","LEFT")
            ->field("a.aid")
            ->alias('a')
            ->where($condition)
            ->order('a.aid desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->getAllWithIndex('aid');

        if ($list) {
            $aids = array_keys($list);
            $fields = "d.*,c.*,b.*, a.*, a.aid as aid";
            $row = $this->archives_db
                ->field($fields)
                ->alias('a')
                ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
                ->join("xinfang_system c","a.aid = c.aid","LEFT")
                ->join("xinfang_content d","a.aid = d.aid","LEFT")
                ->where('a.aid', 'in', $aids)
                ->getAllWithIndex('aid');
            foreach ($list as $key => $val) {
                $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
                $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
                $list[$key] = $row[$val['aid']];
            }
        }
        $show = $Page->show(); // 分页显示输出

        $assign_data['page'] = $show; // 赋值分页输出
        $assign_data['list'] = $list; // 赋值数据集
        $assign_data['pager'] = $Page; // 赋值分页对象
        // 栏目ID
        $assign_data['typeid'] = $typeid; // 栏目ID
        /*当前栏目信息*/
        $arctype_info = array();
        if ($typeid > 0) {
            $arctype_info = M('arctype')->field('typename')->find($typeid);
        }
        $assign_data['arctype_info'] = $arctype_info;
        /*允许发布文档列表的栏目*/
        $arctype_html = allow_release_arctype($typeid, [$this->channeltype]);
        $typeidNum = substr_count($arctype_html, '</option>');
        $assign_data['arctype_html'] = $arctype_html;
        $assign_data['typeidNum'] = $typeidNum;
        //销售情况
        $channelfield = db('channelfield')->where("name='sale_status' and channel_id=9")->find();
        $assign_data['sale_status_list'] = explode(',',$channelfield['dfvalue']);
        $this->assign($assign_data);

        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if (IS_POST) {
            $post = input('post.');
            $typeid = input('post.typeid/d', 0);
            if (empty($typeid)) {
                $this->error('请选择所属栏目！');
            }
            if (empty($post['permission']) && $this->check_title($post['title'])){
                $this->error('已经存在同名楼盘！','',['permission'=>1]);
            }
//            if (empty($post['seo_title'])){
//                $post['seo_title'] = $post['title'];
//            }
            /*获取第一个html类型的内容，作为文档的内容来截取SEO描述*/        
            $contentField = Db::name('channelfield')->where([
                    'channel_id'    => $this->channeltype,
                    'dtype'         => 'htmltext',
                ])->getField('name');
            $content = input('post.addonFieldExt.'.$contentField, '', null);
            /*--end*/

            // 根据标题自动提取相关的关键字
            $seo_keywords = $post['seo_keywords'];
            if (!empty($seo_keywords)) {
                $seo_keywords = str_replace('，', ',', $seo_keywords);
            } else {
                // $seo_keywords = get_split_word($post['title'], $content);
            }

            // 自动获取内容第一张图片作为封面图
            if (empty($post['litpic'])) {
                $post['litpic'] = get_html_first_imgurl($content);
            }
            /*是否有封面图*/
            if (empty($post['litpic'])) {
                $is_litpic = 0; // 无封面图
            } else {
                $is_litpic = 1; // 有封面图
            }

            // SEO描述
            $seo_description = '';
            if (empty($post['seo_description']) && !empty($content)) {
                $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
            } else {
                $seo_description = $post['seo_description'];
            }

            // 外部链接跳转
            $jumplinks = '';
            $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
            if (intval($is_jump) > 0) {
                $jumplinks = $post['jumplinks'];
            }

            // 模板文件，如果文档模板名与栏目指定的一致，默认就为空。让它跟随栏目的指定而变
            if ($post['type_tempview'] == $post['tempview']) {
                unset($post['type_tempview']);
                unset($post['tempview']);
            }

            // --存储数据
            $newData = array(
                'typeid'=> empty($post['typeid']) ? 0 : $post['typeid'],
                'channel'   => $this->channeltype,
                'is_b'      => empty($post['is_b']) ? 0 : $post['is_b'],
                'is_head'      => empty($post['is_head']) ? 0 : $post['is_head'],
                'is_special'      => empty($post['is_special']) ? 0 : $post['is_special'],
                'is_recom'      => empty($post['is_recom']) ? 0 : $post['is_recom'],
                'is_jump'     => $is_jump,
                'is_litpic'     => $is_litpic,
                'jumplinks' => $jumplinks,
                'seo_keywords'     => $seo_keywords,
                'seo_description'     => $seo_description,
                'admin_id'  => session('admin_info.admin_id'),
                'sort_order'    => 100,
                'add_time'     => strtotime($post['add_time']),
                'update_time'  => strtotime($post['add_time']),
                'province_id'  => empty($post['province_id']) ? 0 : $post['province_id'],
                'city_id'      => empty($post['city_id']) ? 0 : $post['city_id'],
                'area_id'      => empty($post['area_id']) ? 0 : $post['area_id'],
                'show_time'      => getTime(),
            );
            $data = array_merge($post, $newData);

            $aid = $this->archives_db->insertGetId($data);
            $_POST['aid'] = $aid;
            if ($aid) {
                // ---------后置操作
                model($this->table)->afterSave($aid, $data, 'add');
                // ---------end
                adminLog('新增数据：'.$data['title']);

                // 生成静态页面代码
                $this->success("操作成功!", url("Index/uphtml").'&aid='.$aid.'&tid='.$post['typeid'].'&controller=Xinfang&action=add');
                exit;
            }

            $this->error("操作失败!");
            exit;
        }

        $typeid = input('param.typeid/d', 0);
        if (empty($typeid)) {
            $arctypeInfo = Db::name('arctype')->where([
                    'current_channel'  => $this->channeltype,
                    'parent_id' => 0,
                    'status'    => 1,
                    'is_del'    => 0,
                ])
                ->order('sort_order asc, id asc')
                ->find();
            $typeid = $arctypeInfo['id'];
        } else {
            $arctypeInfo = Db::name('arctype')->find($typeid);
        }

        /*允许发布文档列表的栏目*/
        $arctype_html = allow_release_arctype($typeid, array($this->channeltype));
        $assign_data['arctype_html'] = $arctype_html;
        /*--end*/
        $assign_data['arctype_html_show'] = substr_count($arctype_html, '</option>') > 1;
        /*自定义字段*/
        $addonFieldExtList = model('Field')->getChannelFieldList($this->channeltype);
        $addonFieldExtList = convert_arr_key($addonFieldExtList,'name');
        $assign_data['addonFieldExtList'] = $addonFieldExtList;
        $assign_data['aid'] = 0;
        /*--end*/
        // 阅读权限
        $arcrank_list = get_arcrank_list();
        $assign_data['arcrank_list'] = $arcrank_list;
        
        /*获取可显示的系统字段*/
        $condition['ifcontrol'] = 0;
        $condition['channel_id'] = $this->channeltype;
        $channelfield_row = Db::name('channelfield')->where($condition)->field('name,ifeditable')->getAllWithIndex('name');
        $assign_data['channelfield_row'] = $channelfield_row;
        /*--end*/
        /*模板列表*/
        $archivesLogic = new \app\admin\logic\ArchivesLogic;
        $templateList = $archivesLogic->getTemplateList($this->nid);
        $this->assign('templateList', $templateList);
        /*--end*/
        /*默认模板文件*/
        $tempview = 'view_'.$this->nid.'.'.config('template.view_suffix');
        !empty($arctypeInfo['tempview']) && $tempview = $arctypeInfo['tempview'];
        $this->assign('tempview', $tempview);
        /*--end*/
        $pen_name = session('admin_info.pen_name');
        if (empty($pen_name)){
            $pen_name = M('Admin')->where("admin_id",session('admin_id'))->getField('user_name');
        }
        $assign_data['author'] = $pen_name;
        //经纪人信息
        $user = new \app\common\model\Users();
        $users_list = $user::get_list(1);
        $assign_data['users_list'] = $users_list;
        $this->assign($assign_data);

        return $this->fetch();
    }
    /**
     * 编辑
     */
    public function edit()
    {
        if (IS_POST) {
            $post = input('post.');
            $typeid = input('post.typeid/d', 0);
            if (empty($typeid)) {
                $this->error('请选择所属栏目！');
            }
            if (empty($post['permission']) && $this->check_title($post['title'],$post['aid'])){
                $this->error('已经存在同名楼盘！','',['permission'=>1]);
            }
            /*获取第一个html类型的内容，作为文档的内容来截取SEO描述*/
            $contentField = Db::name('channelfield')->where([
                    'channel_id'    => $this->channeltype,
                    'dtype'         => 'htmltext',
                ])->getField('name');
            $content = input('post.addonFieldExt.'.$contentField, '', null);
            /*--end*/
            // 根据标题自动提取相关的关键字
            $seo_keywords = $post['seo_keywords'];
            if (!empty($seo_keywords)) {
                $seo_keywords = str_replace('，', ',', $seo_keywords);
            } else {
                // $seo_keywords = get_split_word($post['title'], $content);
            }
            
            // 自动获取内容第一张图片作为封面图
            if (empty($post['litpic'])) {
                $post['litpic'] = get_html_first_imgurl($content);
            }
            /*是否有封面图*/
            if (empty($post['litpic'])) {
                $is_litpic = 0; // 无封面图
            } else {
                $is_litpic = empty($post['is_litpic']) ? 0 : $post['is_litpic']; // 有封面图
            }
            // SEO描述
            $seo_description = '';
            if (empty($post['seo_description']) && !empty($content)) {
                $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
            } else {
                $seo_description = $post['seo_description'];
            }
            // --外部链接
            $jumplinks = '';
            $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
            if (intval($is_jump) > 0) {
                $jumplinks = $post['jumplinks'];
            }

            // 模板文件，如果文档模板名与栏目指定的一致，默认就为空。让它跟随栏目的指定而变
            if ($post['type_tempview'] == $post['tempview']) {
                unset($post['type_tempview']);
                unset($post['tempview']);
            }

            // 同步栏目切换模型之后的文档模型
            $channel = Db::name('arctype')->where(['id'=>$typeid])->getField('current_channel');
            // --存储数据
            $newData = array(
                'typeid'=> $typeid,
                'channel'   => $channel,
                'is_b'      => empty($post['is_b']) ? 0 : $post['is_b'],
                'is_head'      => empty($post['is_head']) ? 0 : $post['is_head'],
                'is_special'      => empty($post['is_special']) ? 0 : $post['is_special'],
                'is_recom'      => empty($post['is_recom']) ? 0 : $post['is_recom'],
                'is_jump'   => $is_jump,
                'is_litpic'     => $is_litpic,
                'jumplinks' => $jumplinks,
                'seo_keywords'     => $seo_keywords,
                'seo_description'     => $seo_description,
                'add_time'     => strtotime($post['add_time']),
                'update_time'     => getTime(),
            );
            $data = array_merge($post, $newData);
            $r = M('archives')->where([
                    'aid'   => $data['aid'],
                ])->update($data);

            if ($r) {
                // ---------后置操作
                model($this->table)->afterSave($data['aid'], $data, 'edit');
                // ---------end
                adminLog('编辑文章：'.$data['title']);
                // 生成静态页面代码
                $this->success("操作成功!", url("Index/uphtml").'&aid='.$data['aid'].'&tid='.$typeid.'&controller=Xinfang&action=edit');
                exit;
            }

            $this->error("操作失败!");
            exit;
        }

        $assign_data = array();

        $id = input('id/d');
        $info = model($this->table)->getInfo($id, null, false);
        if (empty($info)) {
            $this->error('数据不存在，请联系管理员！');
            exit;
        }
        $admin_info = session('admin_info');
        if (0 < intval($admin_info['role_id'])) {
            $auth_role_info = $admin_info['auth_role_info'];
            if(! empty($auth_role_info)){
                if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself'] && $admin_info['admin_id'] != $info['admin_id']){
                    $this->error('您不能操作其他人文档');
                }
                if(!empty($auth_role_info['permission']['arctype']) && !in_array($info['typeid'],$auth_role_info['permission']['arctype'])){
                    $this->error('您没有权限操作该文档数据');
                }
            }
        }
        /*兼容采集没有归属栏目的文档*/
        if (empty($info['channel'])) {
            $channelRow = Db::name('channeltype')->field('id as channel')
                ->where('id',$this->channeltype)
                ->find();
            $info = array_merge($info, $channelRow);
        }
        /*--end*/
        $typeid = $info['typeid'];

        // 栏目信息
        $arctypeInfo = Db::name('arctype')->find($typeid);

        $info['channel'] = $arctypeInfo['current_channel'];
        // SEO描述
        if (!empty($info['seo_description'])) {
            $info['seo_description'] = @msubstr(checkStrHtml($info['seo_description']), 0, config('global.arc_seo_description_length'), false);
        }
        $assign_data['field'] = $info;

        /*允许发布文档列表的栏目，文档所在模型以栏目所在模型为主，兼容切换模型之后的数据编辑*/
        $arctype_html = allow_release_arctype($typeid, array($info['channel']));
        $assign_data['arctype_html'] = $arctype_html;
        /*--end*/
        $assign_data['arctype_html_show'] = substr_count($arctype_html, '</option>') > 1; //count(explode('</option>',$arctype_html)) > 2;

        /*自定义字段*/
        $lng = "";
        $lat = "";
        //获取主表字段信息（archives、system、content三张表）
        $addonFieldExtList = model('Field')->getChannelFieldList($info['channel'], false, $id, $info,"content,system");
        foreach ($addonFieldExtList as $key => $val) {
            if ($val['name'] == 'lng'){
                $lng = $val['dfvalue'];
            }
            if ($val['name'] == 'lat'){
                $lat = $val['dfvalue'];
            }
        }
        if (!empty($lng) && !empty($lat)){
            $assign_data['map'] = $lng.','.$lat;
        }

        $addonFieldExtList = convert_arr_key($addonFieldExtList,'name');
        $assign_data['addonFieldExtList'] = $addonFieldExtList;
        $assign_data['aid'] = $id;
        /*--end*/
        // 阅读权限
        $arcrank_list = get_arcrank_list();
        $assign_data['arcrank_list'] = $arcrank_list;
        /*模板列表*/
        $archivesLogic = new \app\admin\logic\ArchivesLogic;
        $templateList = $archivesLogic->getTemplateList($this->nid);
        $this->assign('templateList', $templateList);
        /*--end*/

        /*默认模板文件*/
        $tempview = $info['tempview'];
        empty($tempview) && $tempview = $arctypeInfo['tempview'];
        $this->assign('tempview', $tempview);
        /*--end*/
        //读取相册信息
        $photo_list = model("xinfang_photo")->getListByWhere(['aid'=>$id,'is_del'=>0]);
        $assign_data['photo_list'] = $photo_list;
        //读取户型信息
        $huxing_list = model("xinfang_huxing")->getListByWhere(['aid'=>$id,'is_del'=>0]);
        $assign_data['huxing_list'] = $huxing_list;
        $assign_data['huxing_count'] = count($huxing_list);
        //读取沙盘信息
        $sand_list = Db::name("xinfang_sand")->where(['aid'=>$id,'is_del'=>0])->select();
        $assign_data['sand_list'] = $sand_list;
        $this->getAllPoint($id);
        //经纪人信息
        $user = new \app\common\model\Users();
        $users_list = $user::get_list(1,$info['users_id']);
        $assign_data['users_list'] = $users_list;

        $this->assign($assign_data);

        return $this->fetch();
    }
    private function getAllPoint($aid){
        $where['aid'] = $aid;
        $detail = Db::name("xinfang_sand_pic")->where($where)->find();
        $id_arr = [];
        $uri    = url('Xinfang/addSandPic');
        if($detail)
        {
            if($detail['data'])
            {
                $data = json_decode($detail['data'],true);
                foreach($data as $v)
                {
                    $id_arr[$v['sand_id']] = $v['point'];

                }
            }
        }
        $this->assign('uri',$uri);
        $this->assign('select_points',array_keys($id_arr));
        $this->assign('init_points',$id_arr);
        $this->assign('points',$detail);
    }
    
    /**
     * 删除
     */
    public function del()
    {
        $archivesLogic = new \app\admin\logic\ArchivesLogic;
        $archivesLogic->del();
    }

    /*
     * js打开获取楼盘列表
     */
    public function ajaxSelectHouse(){
        $channel= input('channel/d', 9);
        $typeid = input('typeid/d');
        $keywords = input('keywords/s');
        $func = input('func/s');
        $condition = array();
        $condition['a.channel'] = $channel;
        if ($typeid){
            $condition['a.typeid'] = $typeid;
        }
        if ($keywords){
            $condition['a.title'] =  array('LIKE', "%{$keywords}%");;
        }
        $assign_data = $this->getLists($condition);
        $assign_data['func'] = $func;
        $this->assign($assign_data);

        return $this->fetch();
    }

    /**
     * ajax获取楼盘列表数据
     */
    public function getListData(){
        $condition['a.channel'] = 9;
        $assign_data = $this->getLists($condition);

        return json_encode($assign_data['list']);
    }

    /**
     * 获取列表数据
     */
    private function getLists($condition){
        /*
         * 数据查询，搜索出主键ID的值
         */
        $count = DB::name('archives')->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数

        $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
        $list = DB::name('archives')
            ->field("a.aid")
            ->alias('a')
            ->where($condition)
            ->order('a.aid desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->getAllWithIndex('aid');
        /*
         * 完善数据集信息
         * 在数据量大的情况下，经过优化的搜索逻辑，先搜索出主键ID，再通过ID将其他信息补充完整；
         */
        if ($list) {
            $aids = array_keys($list);
            $fields = "d.*,c.*,b.*, a.*, a.aid as aid";
            $row = DB::name('archives')
                ->field($fields)
                ->alias('a')
                ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
                ->join('xinfang_content c','a.aid = c.aid')
                ->join('xinfang_system d','a.aid = d.aid')
                ->where('a.aid', 'in', $aids)
                ->getAllWithIndex('aid');
            $region = DB::name("region")->where("level=1 or level=2")->getField("id,name");
            foreach ($list as $key => $val) {
                $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
                $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
                $row[$val['aid']]['city'] = !empty($region[$row[$val['aid']]['city_id']])?$region[$row[$val['aid']]['city_id']]:'';
                $row[$val['aid']]['area'] = !empty($region[$row[$val['aid']]['area_id']])?$region[$row[$val['aid']]['area_id']]:'';
                $row[$val['aid']]['province'] =  !empty($region[$row[$val['aid']]['province_id']])?$region[$row[$val['aid']]['province_id']]:'';
                $list[$key] = $row[$val['aid']];
            }
        }
        $show = $Page->show(); // 分页显示输出
        $assign_data['page'] = $show; // 赋值分页输出
        $assign_data['list'] = $list; // 赋值数据集
        $assign_data['pager'] = $Page; // 赋值分页对象

        return $assign_data;
    }
    /*
     * 添加楼栋信息
     */
    public function sand_add(){
        if (IS_POST){
            $post = input('post.');
            $aid = input('post.aid/d', 0);
            if (empty($aid)) {
                $this->error('请选择所属楼盘！');
            }
            !empty($post['huxing_id']) && $post['huxing_id'] = implode(',',$post['huxing_id']);
            // --存储数据
            $newData = array(
                'open_time' => !empty($post['open_time']) ? strtotime($post['open_time']) : 0,
                'complate_time' => !empty($post['complate_time']) ? strtotime($post['complate_time']) : 0,
                'add_time'     => getTime(),
                'update_time'  => getTime(),
            );
            $data = array_merge($post, $newData);
            $r = Db::name("xinfang_sand")->insertGetId($data);
            if ($r) {
                // ---------后置操作
                // ---------end
                adminLog('新增数据：'.$data['title']);

                // 生成静态页面代码
                $data['sand_id'] = $r;
                $this->success("操作成功!", '',$data);
                exit;
            }

            $this->error("操作失败!");
            exit;
        }
        $aid = input('aid/d');
        $huxing_list = Db::name("xinfang_huxing")->where("aid={$aid} and is_del=0")->select();
        $assign_data['aid'] = $aid;
        $assign_data['huxing_list'] = $huxing_list;
        $this->assign($assign_data);

        return $this->fetch();
    }
    /*
     * 编辑楼栋
     */
    public function sand_edit(){
        if (IS_POST){
            $post = input('post.');
            $aid = input('post.aid/d', 0);
            if (empty($aid)) {
                $this->error('请选择所属楼盘！');
            }
            !empty($post['huxing_id']) && $post['huxing_id'] = implode(',',$post['huxing_id']);
            // --存储数据
            $newData = array(
                'open_time' => !empty($post['open_time']) ? strtotime($post['open_time']) : 0,
                'complate_time' => !empty($post['complate_time']) ? strtotime($post['complate_time']) : 0,
                'update_time'  => getTime(),
            );
            $data = array_merge($post, $newData);
            $r = Db::name("xinfang_sand")->where([
                'sand_id'   => $data['sand_id'],
            ])->update($data);
            if ($r) {
                // ---------后置操作
                // ---------end
                adminLog('更新数据：'.$data['title']);

                // 生成静态页面代码
                $this->success("操作成功!", '',$data);
                exit;
            }

            $this->error("操作失败!");
            exit;
        }
        $aid = input('aid/d');
        $sand_id = input('sand_id/d');
        $assign_data['aid'] = $aid;
        $list = Db::name("xinfang_sand")->where("aid={$aid} and sand_id={$sand_id}")->find();
        $list['huxing_id'] = !empty($list['huxing_id']) ? explode(',',$list['huxing_id']) : '';
        $assign_data['field'] = $list;
        $huxing_list = Db::name("xinfang_huxing")->where("aid={$aid} and is_del=0")->select();
        $assign_data['huxing_list'] = $huxing_list;
        $this->assign($assign_data);

        return $this->fetch();
    }
    /*
     * 删除楼栋
     */
    public function sand_del(){
        $id_arr = input('del_id/a');
        $id_arr = eyIntval($id_arr);
        if(IS_POST && !empty($id_arr)){
            $r = Db::name('xinfang_sand')->where('sand_id','IN',$id_arr)->delete();
            if ($r) {
                adminLog('删除楼栋内容-id：'.implode(',', $id_arr));
                $this->success('删除成功','',$id_arr);
            } else {
                $this->error('删除失败');
            }
        }else{
            $this->error('参数有误');
        }
    }
    /*
     * 保存沙盘
     */
    public function addSandPic(){
        $post = input('post.');
        if (empty($post['aid'])){
            $this->error('参数错误');
        }
        $have = Db::name('xinfang_sand_pic')->where("aid={$post['aid']}")->find();
        if ($have){     //编辑
            $newData = array(
                'data' => json_encode($post['data']),
                'update_time'  => getTime(),
            );
            $data = array_merge($post, $newData);
            $r = Db::name("xinfang_sand_pic")->where([
                'aid'   => $data['aid'],
            ])->update($data);
            if ($r) {
                // ---------后置操作
                // ---------end
                adminLog('更新数据：'.$data['title']);

                // 生成静态页面代码
                $this->success("操作成功!");
                exit;
            }
        }else{      //添加
            $newData = array(
                'add_time'     => getTime(),
                'update_time'  => getTime(),
                'data' => json_encode($post['data']),
            );
            $data = array_merge($post, $newData);
            $r = Db::name("xinfang_sand_pic")->insertGetId($data);
            if ($r) {
                // ---------后置操作
                // ---------end
                adminLog('新增数据：'.$data['aid']);

                // 生成静态页面代码
                $this->success("操作成功!");
                exit;
            }
        }
        $this->error("操作失败!");
        exit;
    }
    /*
     * 删除沙盘
     */
    public function sand_pic_del(){
        $id = input('id/d');
        if(IS_POST && !empty($id)){
            $r = Db::name('xinfang_sand_pic')->where("id={$id}")->delete();
            if ($r) {
                adminLog('删除楼栋内容-id：'.$id);
                $this->success('删除成功','',$id);
            } else {
                $this->error('删除失败');
            }
        }else{
            $this->error('参数有误');
        }
    }
    //检查是否存在相同名称楼盘
    private function check_title($title,$aid = 0){
        $where = ["title"=>['eq',$title],'channel'=>$this->channeltype];
        if ($aid){
            $where['aid'] = ['neq',$aid];
        }
        $have = Db::name("archives")->where($where)->find();
        if ($have){
            return true;
        }

        return false;
    }
}